<template>
    <div>
        <el-breadcrumb separator="/" style="padding-left:10px;padding-bottom:10px;font-size:12px;">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{this.$router.currentRoute.name}}</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 用户列表卡片区 -->
        <el-card class="box-card">
            <el-form :inline="true" ref="form" :model="queryMap" label-width="70px" size="small">
                <el-form-item label="班级名称">
                    <el-input
                            @keyup.enter.native="searchClass"
                            @clear="searchClass"
                            clearable
                            v-model="queryMap.classname"
                            placeholder="请输入班级名称查询"
                    ></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select
                            clearable
                            v-model="queryMap.clsstatus"
                            @clear="searchClass"
                            placeholder="请选择状态"
                    >
                        <el-option label="正常" value="1"></el-option>
                        <el-option label="已合班" value="2"></el-option>
                        <el-option label="已就业" value="3"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item style="margin-left:50px;">
                    <el-button  @click="reset" icon="el-icon-refresh">重置</el-button>
                    <el-button type="primary" @click="searchClass" icon="el-icon-search">查询</el-button>
                </el-form-item>
            </el-form>
            <div style="margin-bottom: 10px;">
                <el-button
                        type="success"
                        icon="el-icon-plus"
                        @click="addDialogVisible=true"
                        v-hasPermission="'user:add'"
                >添加</el-button>
                <el-button @click="downExcel" v-hasPermission="'user:export'"  icon="el-icon-download">导出</el-button>
            </div>

            <!-- 表格区域 -->
            <el-table v-loading="loading" size="small" :data="classList" border style="width: 100%;" height="420">
                <!-- <el-table-column type="selection" width="40"></el-table-column> -->
                <el-table-column label="#" prop="classid" width="50"></el-table-column>
                <el-table-column prop="classname" label="班级名称" width="110"></el-table-column>
                <el-table-column prop="headmaster" label="班主任" width="110">
                    <template slot-scope="scope">
                        <span>{{scope.row.master}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="instructor" label="教员" width="110">
                    <template slot-scope="scope">
                        <span>{{scope.row.instname}}</span>
                    </template>
                </el-table-column>

                <el-table-column prop="employmentguide" label="就业老师" width="100" sortable>
                    <template slot-scope="scope">
                        <span>{{scope.row.guidename}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" width="150">
                    <template slot-scope="scope">
                        <i class="el-icon-time"></i>
                        <span style="margin-left: 10px">{{ scope.row.createtime }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="isban" label="班级状态" width="100">
                    <template slot-scope="scope">
                        <span>{{clsStatus(scope.row.clsstatus)}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="备注" width="200">
                    <template slot-scope="scope">
                        <!-- 光标放在文字上面 悬出内容区域 -->
                        <el-popover trigger="hover" placement="top">
                            <p>备注: {{ scope.row.remarks }}</p>
                            <div slot="reference" class="name-wrapper">
                                <el-tag size="medium">{{ scope.row.remarks }}</el-tag>
                            </div>
                        </el-popover>
                    </template>
                </el-table-column>
                <el-table-column label="操作" >
                    <template slot-scope="scope">
                        <el-button size="small" type="primary" icon="el-icon-edit-outline" @click="edit(scope.row.classid)"></el-button>
                        <el-button type="danger" size="small" icon="el-icon-delete" @click="del(scope.row.classid)"></el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页 -->
            <el-pagination
                    style="margin-top:10px;"
                    background
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="queryMap.pageNo"
                    :page-sizes="[6, 10, 20, 30]"
                    :page-size="queryMap.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
            ></el-pagination>


            <!-- 添加对话框 -->
            <el-dialog :title="title" @close="closeDialog" :visible.sync="addDialogVisible" width="50%;">
                <!-- 表单 -->
                <span>
          <el-form
                  :model="addForm"
                  :label-position="labelPosition"
                  :rules="addFormRules"
                  ref="addFormRef"
                  label-width="80px"
          >
            <el-row>
              <el-col :span="10">
                <div class="grid-content bg-purple">
                  <el-form-item label="班级名称" prop="classname">
                    <el-input v-model="addForm.classname"></el-input>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="12">
                <div class="grid-content bg-purple-light">
                <el-form-item label="创建时间">
                  <el-date-picker
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择日期"
                          v-model="addForm.createtime"
                          style="width: 100%"
                  ></el-date-picker>
                </el-form-item>

                </div>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="10">
                <div class="grid-content bg-purple-light">
                  <el-form-item label="教员">
                    <el-select v-model="addForm.instructor" placeholder="请选择教员">
                      <el-option
                              v-for="emp in emps"
                              :key="emp.empid"
                              :label="emp.empname"
                              :value="emp.empid"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="10">
                <div class="grid-content bg-purple-light">
                  <el-form-item label="就业老师" >
                    <el-select v-model="addForm.employmentguide" placeholder="请选择就业老师">
                      <el-option
                              v-for="emp in emps"
                              :key="emp.empid"
                              :label="emp.empname"
                              :value="emp.empid"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-col>
            </el-row>
               <el-row>
              <el-col :span="10">
                <div class="grid-content bg-purple-light">
                    <el-form-item label="班主任">
                    <el-select v-model="addForm.headmaster" placeholder="请选择班主任">
                      <el-option
                              v-for="emp in emps"
                              :key="emp.empid"
                              :label="emp.empname"
                              :value="emp.empid"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="10">
                <div class="grid-content bg-purple-light">
                 <el-form-item label="状态">
                    <el-select
                            clearable
                            v-model="addForm.clsstatus"
                            placeholder="请选择状态"
                    >
                        <el-option label="正常" :value="1"></el-option>
                        <el-option label="已合班" :value="2"></el-option>
                        <el-option label="已就业" :value="3"></el-option>
                    </el-select>
                </el-form-item>
                </div>
              </el-col>
            </el-row>

            <el-row>
                <el-form-item label="备注">
              <el-input type="textarea" v-model="addForm.remarks"></el-input>
            </el-form-item>
            </el-row>

          </el-form>
        </span>

                <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button
                  type="primary"
                  @click="addClass"
                  :loading="btnLoading"
                  :disabled="btnDisabled"
          >确 定</el-button>
        </span>
            </el-dialog>

        </el-card>
    </div>
</template>
<script>
    import axios from "axios";
    export default {
        data() {
           return {
                title:'添加班级',
                btnLoading: false,
                btnDisabled: false,
                emps: [],
                loading: true,
                total: 0,
                addDialogVisible: false, //添加对话框,
                editDialogVisible: false, //修改对话框
                assignDialogVisible: false, //分配角色对话框
                labelPosition: "right", //lable对齐方式
                //查询对象
                queryMap: {
                    pageNum: 1,
                    pageSize: 6,
                    classname: "",
                    clsstatus: "",
                },
               classList: [],
               getname:'',
               addForm:{
                   classname:'',
                   createtime: '',
                   headmaster:'',
                   instructor:'',
                   employmentguide:'',
                   clsstatus:'',
                   remarks:''
               },
               addFormRules: {
                   classname: [
                       {required: true, message: "请输入班级名", trigger: "blur"},
                       {min: 2, max: 10, message: "长度在 2 到 10 个字符", trigger: "blur"}
                   ],
               }
            };
        },
        methods: {
            getName(id){
                let empName = '';
                this.$http.get('/class/getEmpNameById/'+id).then((res) =>{
                    this.getname = res.data.data
                })
                console.log(this.getname)
                return this.getname;
            },
            clsStatus(clsstatus){
                if(clsstatus ===1){
                    return "正常"
                }else if(clsstatus ===2){
                    return "已合班"
                }else{
                    return "已就业"
                }
            },
            /**
             * 重置
             */
            reset(){
                this.queryMap= {
                    pageNum: 1,
                    pageSize: 6,
                    className: "",
                    clsStatus: ""
                };
            },
            /**
             * 加载用户表格
             */
            downExcel() {
                const $this = this;
                const res = axios
                    .request({
                        url: "http://localhost:8989/class/excel",
                        method: "post",
                        responseType: "blob",
                        headers:{'Authorization':LocalStorage.get(LOCAL_KEY_XINGUAN_ACCESS_TOKEN)},	//设置请求头
                    })
                    .then(res => {
                        if (res.headers["content-type"] === "application/json") {
                            return $this.$message.error(
                                "Subject does not have permission [class:export]"
                            );
                        }
                        const data = res.data;
                        let url = window.URL.createObjectURL(data); // 将二进制文件转化为可访问的url
                        const a = document.createElement("a");
                        document.body.appendChild(a);
                        a.href = url;
                        a.download = "班级列表.xls";
                        a.click();
                        window.URL.revokeObjectURL(url);
                    });
            },


            /**
             * 加载班级列表
             */
            async getClassList() {
                const { data: res } = await this.$http.get("/class/findList", {
                    params: this.queryMap
                });
                if(!res.success){
                    return this.$message.error("获取班级列表失败:"+res.data.errorMsg);
                }
                this.total = res.data.total;
                this.classList = res.data.rows;
                this.loading = false;
            },

            /**
             * 删除班级
             */
            async del(classid) {
                const res = await this.$confirm(
                    "此操作将永久删除该班级, 是否继续?",
                    "提示",
                    {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning"
                    }
                ).catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除"
                    });
                });
                if (res === "confirm") {
                    const { data: res } = await this.$http.delete("/class/delete/" + classid);
                    console.log(res);
                    if(res.success){
                        this.$notify.success({
                            title:'操作成功',
                            message:'班级删除成功',
                        });
                        await this.getClassList();
                        await this.getEmpList();
                    }else {
                        this.$message.error(res.data.errorMsg);
                    }
                }
            },
            /**
             * 添加用户
             */
            async addClass() {
                this.$refs.addFormRef.validate(async valid => {
                    if (!valid) {
                        return;
                    } else {
                        if(this.addForm.classid){
                            this.updateClass()
                        }else{
                            this.saveClass()
                        }
                    }
                });
            },
            async saveClass(){
                this.btnLoading = true;
                this.btnDisabled = true;
                const { data: res } = await this.$http.post("/class/add", this.addForm);
                if(res.success){
                    this.$notify.success({
                        title:'操作成功',
                        message:'班级添加成功',
                    });
                    this.addForm = {};
                    await this.getClassList();
                    await this.getEmpList();
                } else {
                    return this.$message.error("班级添加失败:" + res.data.errorMsg);
                }
                this.addDialogVisible = false;
                this.btnLoading = false;
                this.btnDisabled = false;
            },
            /**
             * 更新班级
             */
            async updateClass() {
                this.btnLoading = true;
                this.btnDisabled = true;
                const { data: res } = await this.$http.put(
                    "/class/update/" + this.addForm.classid,
                    this.addForm
                );
                if(res.success){
                    this.$notify({
                        title: "操作成功",
                        message: "班级基本信息已更新",
                        type: "success"
                    });
                    this.addForm = {};
                    await this.getClassList();
                    await this.getEmpList();
                } else {
                    this.$message.error("班级信息更新失败:" + res.data.errorMsg);
                }
                this.addDialogVisible = false;
                this.btnLoading = false;
                this.btnDisabled = false;
            },
            /**
             * 搜索用户
             */
            searchClass() {
                this.queryMap.pageNum = 1;
                this.getClassList();
            },
            /**
             * 修改用户信息
             */
            async edit(classid) {
                this.title ="编辑班级信息"
                const { data: res } = await this.$http.get("/class/edit/" + classid);
                if(res.success){
                    this.addForm = res.data;
                    this.addDialogVisible = true;
                } else {
                    return this.$message.error("班级信息编辑失败:" + res.data.errorMsg);
                }
            },
            /**
             *  改变页码
             */
            handleSizeChange(newSize) {
                this.queryMap.pageSize = newSize;
                this.getClassList();
            },
            /**
             * 翻页
             */
            handleCurrentChange(current) {
                this.queryMap.pageNum = current;
                this.getClassList();
            },

            /**
             * 关闭添加弹出框
             */
            closeDialog() {
                this.$refs.addFormRef.clearValidate();
                this.addForm.birth = "";
                this.addForm = {};
            },
            /**
             * 关闭编辑弹出框
             */
            editClose() {
                this.$refs.editFormRef.clearValidate();
                this.editForm = {};
            },

            /**
             * 加载所有部门
             */
            async getEmpList() {
                const { data: res } = await this.$http.get("/emp/getEmpNameList");
                if(!res.success){
                    return this.$message.error("获取员工列表失败:"+res.data.errorMsg);
                }
                this.emps = res.data;
            },
        },
        created() {
            this.getClassList();
            this.getEmpList();
            // setTimeout(() => {
            //     this.loading = false;
            // }, 500);
        }
    };
</script>
